''' board module'''

import random
from itertools import product
import utility 
import location 
import game_piece 
import game_card 
import game_constants

def make_house():

    flowshuffle = utility.update_to_dataflow(random.shuffle)

    def connect_rooms(room_pos, room1, room2, orientation, dir1, dir2, position, game_piece, ss_position):

        s = location.StartingSquare(game_piece, ss_position) if game_piece else None
        h = location.Hallway(orientation, (dir1, room1), (dir2, room2), position, s)
        room_pos[ position ] = h

        ### ugly hack, consider how to fix this ###
        if s:
            s.hallway = h
            room_pos[ ss_position ] = s

        room1.connect_entrance(dir2, h)
        room2.connect_entrance(dir1, h)






    def connect_rooms_ew(room_pos, west_room, east_room, position, game_piece = None, ss_position = ()):
        connect_rooms(room_pos, west_room, east_room, 'EW', 'W', 'E', position, game_piece, ss_position)

    def connect_rooms_ns(room_pos, north_room, south_room, position, game_piece = None, ss_position = ()):
        connect_rooms(room_pos, north_room, south_room, 'NS', 'N', 'S', position, game_piece, ss_position)

    #create game pieces
    players = dict((p, game_piece.PlayerPiece(p)) for p in game_constants.g_characters.iterkeys())
    weapons = dict((w, game_piece.WeaponPiece(w)) for w in game_constants.g_weapons)

    # create rooms
    rooms = dict((r, location.Room(r, l)) for (r,l) in game_constants.g_rooms.iteritems())
    room_pos = dict((l, rooms[r]) for (r,l) in game_constants.g_rooms.iteritems())  # reverse lookup by position

    # mix weapons in rooms
    for (w,r) in zip(flowshuffle(weapons.itervalues()), flowshuffle(rooms.itervalues())):
        r._enter(w)
        w.move_piece(r)


    # place pieces on board
    study    = rooms[ 'Study' ]
    hall     = rooms[ 'Hall' ]
    lounge   = rooms[ 'Lounge' ]
    library  = rooms[ 'Library' ] 
    billiard = rooms[ 'Billiard Room' ]
    conserv  = rooms[ 'Conservatory' ]
    ball     = rooms[ 'Ball Room' ]
    kitchen  = rooms[ 'Kitchen' ]
    dining   = rooms[ 'Dining Room' ]

    scarlet = players[ 'Miss Scarlet' ]
    green   = players[ 'Mr. Green' ]
    white   = players[ 'Mrs. White' ]
    plum    = players[ 'Prof. Plum' ]
    mustard = players[ 'Col. Mustard' ]
    peacock = players[ 'Mrs. Peacock' ]

    # Setup East / West connections
    connect_rooms_ew(room_pos, study, hall,       (1,2))
    connect_rooms_ew(room_pos, hall, lounge,      (1,4), scarlet, (0,4))
    connect_rooms_ew(room_pos, library, billiard, (3,2))
    connect_rooms_ew(room_pos, billiard, dining,  (3,4))
    connect_rooms_ew(room_pos, conserv, ball,     (5,2), green, (6,2))
    connect_rooms_ew(room_pos, ball, kitchen,     (5,4), white, (6,4))
    

    # Setup North / South connections
    connect_rooms_ns(room_pos, study, library,  (2,1), plum, (2,0))
    connect_rooms_ns(room_pos, hall, billiard,  (2,3))
    connect_rooms_ns(room_pos, lounge, dining,  (2,5), mustard, (2,6))
    connect_rooms_ns(room_pos, library, conserv,(4,1), peacock, (4,0))
    connect_rooms_ns(room_pos, billiard, ball,  (4,3))
    connect_rooms_ns(room_pos, dining, kitchen, (4,5))


    # Setup Cards
    weapon_cards = flowshuffle([game_card.WeaponCard(n) for n in game_constants.g_weapons])
    room_cards   = flowshuffle([game_card.RoomCard(n) for n in game_constants.g_rooms.iterkeys()])
    char_cards   = flowshuffle([game_card.CharacterCard(n) for n in game_constants.g_characters.iterkeys()])


    answer = (char_cards.pop(),
              weapon_cards.pop(),
              room_cards.pop())

    deck = flowshuffle(weapon_cards + room_cards + char_cards)


    return (room_pos, rooms, players, weapons, deck, answer)


if __name__ == '__main__':
    pass

